package jp.co.nes.awssupport.business.logic.description;

import jp.co.nes.awssupport.business.bean.BillingCsvBean;
import jp.co.nes.awssupport.common.bean.ReservedManageBean;

/**
 * ElastiCache用の説明作成です。
 */
public class AmazonElastiCacheDescriptionImpl extends BaseDescriptionImpl {

    /**
     * デフォルトコンストラクタです。
     */
    public AmazonElastiCacheDescriptionImpl() {
    }

    /*
     * (非 Javadoc)
     * @see jp.co.nes.awssupport.business.logic.description.BaseDescriptionImpl#createDescriptionForReserved(jp.co.nes.awssupport.business.bean.BillingCsvBean, jp.co.nes.awssupport.common.bean.ReservedManageBean)
     */
    @Override
    protected String createDescriptionForReserved(BillingCsvBean csvBean, ReservedManageBean rmBean) {
        // TODO デフォルトは使用タイプでよいか？
        //String result = csvBean.getLineItemDescription();
        String result = csvBean.getUsageType();

        String productName = csvBean.getProductName();

        String regionCode = rmBean.getRegionCode();

        String reservedInstanceValue = getReservedInstanceValue(productName);
        if (reservedInstanceValue == null) {
            addWarnMessage(csvBean);
            return result;
        }

        String operationValue = getOperationValueForReserved(productName, rmBean);
        if (operationValue == null) {
            addWarnMessage(csvBean);
            return result;
        }

        String instanceType = rmBean.getInstanceType();
        if (instanceType == null) {
            addWarnMessage(csvBean);
            return result;
        }

        String durationValue = descriptionInfoHelper.get(productName, Integer.toString(rmBean.getDuration()));
        if (durationValue == null) {
            addWarnMessage(csvBean);
            return result;
        }

        String offeringTypeValue = descriptionInfoHelper.get(productName, rmBean.getOfferingType());
        if (offeringTypeValue == null) {
            addWarnMessage(csvBean);
            return result;
        }

        String lineItemTypeValue = getLineItemTypeValueForReserved(productName, csvBean.getLineItemType());
        if (lineItemTypeValue == null) {
            addWarnMessage(csvBean);
            return result;
        }

        StringBuilder builder = new StringBuilder();
        builder.append(getDescriptionPrefix(regionCode));
        builder.append(reservedInstanceValue);
        builder.append(FWC).append(operationValue);
        builder.append(FWC).append(instanceType);
        builder.append(FWC).append(durationValue);
        builder.append(FWC).append(offeringTypeValue);
        builder.append(FWC).append(lineItemTypeValue);
        result = builder.toString();
        return result;
    }
}
